{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 98,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "28.25\n",
      "77.5\n",
      "93.25\n",
      "93.75\n",
      "['ZhangFei' 'GuanYu' 'ZhaoYun' 'HuangZhong']\n",
      "[32 24 28 29]\n",
      "[75 85 85 65]\n",
      "[100  96  92  85]\n",
      "[ 90.   88.5  96.5 100. ]\n",
      "[('ZhangFei', 32, 75, 100,  90. ) ('GuanYu', 24, 85,  96,  88.5)\n",
      " ('ZhaoYun', 28, 85,  92,  96.5) ('HuangZhong', 29, 65,  85, 100. )]\n"
     ]
    },
    {
     "ename": "IndexError",
     "evalue": "too many indices for array",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mIndexError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-98-30558e3ef830>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m     19\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0menglishs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     20\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mpeoples\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 21\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mpeoples\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;31m#不支持 此种切片索引因为数组是peoples是一维的\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mIndexError\u001b[0m: too many indices for array"
     ]
    }
   ],
   "source": [
    "#  自定义结构数组\n",
    "import numpy as np\n",
    "persontype = np.dtype({'names':['name', 'age', 'chinese', 'math', 'english']#自定义命名空间, \n",
    "\t\t\t\t\t\t\t,'formats':['U32','i', 'i', 'i', 'f']})\n",
    "peoples = np.array([(\"ZhangFei\",32,75,100, 90),(\"GuanYu\",24,85,96,88.5), \n",
    "\t\t\t\t\t\t(\"ZhaoYun\",28,85,92,96.5),(\"HuangZhong\",29,65,85,100)], dtype=persontype)\n",
    "ages = peoples['age']\n",
    "chineses = peoples['chinese']\n",
    "maths = peoples['math']\n",
    "englishs = peoples['english']\n",
    "print(np.mean(ages))\n",
    "print(np.mean(chineses))\n",
    "print(np.mean(maths))\n",
    "print(np.mean(englishs))\n",
    "print(peoples['name'])\n",
    "print(ages)\n",
    "print(chineses)\n",
    "print(maths)\n",
    "print(englishs)\n",
    "print(peoples)\n",
    "print(peoples[0][1:])#不支持 此种切片索引因为数组是peoples是一维的"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[(b'ZhangFei', 74.25), (b'GuanYu', 73.375), (b'ZhaoYun', 75.375), (b'HuangZhong', 69.75)]\n",
      "[(b'HuangZhong', 69.75), (b'GuanYu', 73.375), (b'ZhangFei', 74.25), (b'ZhaoYun', 75.375)]\n"
     ]
    }
   ],
   "source": [
    "Meanlist = []\n",
    "for i  in range(peoples.size):\n",
    "    a = np.mean(list(peoples[i])[1:])\n",
    "    Meanlist.append((peoples[i][0],a))\n",
    "print(Meanlist)\n",
    "print(sorted(Meanlist,key=lambda x:x[1]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 's32' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-55-7dd6262c0288>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[1;31m#自定义数据类型\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mdt\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mdtype\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0ms32\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mi\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mi\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mi\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mf\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      3\u001b[0m hero = np.array([[\"ZhangFei\",32,75,100, 90],[\"GuanYu\",24,85,96,88.5], \n\u001b[0;32m      4\u001b[0m                      [\"ZhaoYun\",28,85,92,96.5],[\"HuangZhong\",29,65,85,100]],dtype=dt)\n\u001b[0;32m      5\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mhero\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mNameError\u001b[0m: name 's32' is not defined"
     ]
    }
   ],
   "source": [
    "#自定义数据类型\n",
    "#下面的代码格式不对\n",
    "dt = np.dtype([[s32,i,i,i,f]])\n",
    "hero = np.array([[\"ZhangFei\",32,75,100, 90],[\"GuanYu\",24,85,96,88.5], \n",
    "                     [\"ZhaoYun\",28,85,92,96.5],[\"HuangZhong\",29,65,85,100]],dtype=dt)\n",
    "print(hero)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[(b'HuangZhong', 69.75), (b'GuanYu', 73.375), (b'ZhangFei', 74.25), (b'ZhaoYun', 75.375)]\n",
      "[(b'ZhaoYun', 75.375), (b'ZhangFei', 74.25), (b'GuanYu', 73.375), (b'HuangZhong', 69.75)]\n"
     ]
    }
   ],
   "source": [
    "print(Meanlist)\n",
    "Meanlist.sort(key = lambda x:x[1],reverse=True)\n",
    "print(Meanlist)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[(b'ZhangFei', 32, 75, 100,  90. ) (b'GuanYu', 24, 85,  96,  88.5)\n",
      " (b'ZhaoYun', 28, 85,  92,  96.5) (b'HuangZhong', 29, 65,  85, 100. )]\n"
     ]
    }
   ],
   "source": [
    "print(peoples)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "数学最高分是 ZhangFei 100 分\n"
     ]
    }
   ],
   "source": [
    "print(peoples['math'].argmax())\n",
    "print('数学最高分是',peoples[peoples['math'].argmax()][0],peoples['math'].max(),'分')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
